<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>逐步匹配多幅点云 | 荒岛</title><meta name="author" content="Fi9zero"><meta name="copyright" content="Fi9zero"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="PCL 点云配准📌 1. 核心目标实现 增量式点云配准（Pairwise Incremental Registration），将多个 .pcd 点云依次配准并拼接成一个完整模型。  📌 2. 主要流程   步骤 说明    ① 加载数据 从命令行读取多个 .pcd 文件，存入 std::vector&lt;PCD&gt;   ② 预处理 下采样（VoxelGrid）+ 去除 NaN 点   ③">
<meta property="og:type" content="article">
<meta property="og:title" content="逐步匹配多幅点云">
<meta property="og:url" content="https://ckyfi9zero.github.io/2025/08/04/2025-08-04-%E9%80%90%E6%AD%A5%E5%8C%B9%E9%85%8D%E5%A4%9A%E5%B9%85%E7%82%B9%E4%BA%91/index.html">
<meta property="og:site_name" content="荒岛">
<meta property="og:description" content="PCL 点云配准📌 1. 核心目标实现 增量式点云配准（Pairwise Incremental Registration），将多个 .pcd 点云依次配准并拼接成一个完整模型。  📌 2. 主要流程   步骤 说明    ① 加载数据 从命令行读取多个 .pcd 文件，存入 std::vector&lt;PCD&gt;   ② 预处理 下采样（VoxelGrid）+ 去除 NaN 点   ③">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://ckyfi9zero.github.io/image/avatar.jpg">
<meta property="article:published_time" content="2025-08-04T01:56:31.000Z">
<meta property="article:modified_time" content="2025-08-04T02:05:10.507Z">
<meta property="article:author" content="Fi9zero">
<meta property="article:tag" content="c++">
<meta property="article:tag" content="PCL">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://ckyfi9zero.github.io/image/avatar.jpg"><script type="application/ld+json">{
  "@context": "https://schema.org",
  "@type": "BlogPosting",
  "headline": "逐步匹配多幅点云",
  "url": "https://ckyfi9zero.github.io/2025/08/04/2025-08-04-%E9%80%90%E6%AD%A5%E5%8C%B9%E9%85%8D%E5%A4%9A%E5%B9%85%E7%82%B9%E4%BA%91/",
  "image": "https://ckyfi9zero.github.io/image/avatar.jpg",
  "datePublished": "2025-08-04T01:56:31.000Z",
  "dateModified": "2025-08-04T02:05:10.507Z",
  "author": [
    {
      "@type": "Person",
      "name": "Fi9zero",
      "url": "https://ckyfi9zero.github.io"
    }
  ]
}</script><link rel="shortcut icon" href="/image/favicon.ico"><link rel="canonical" href="https://ckyfi9zero.github.io/2025/08/04/2025-08-04-%E9%80%90%E6%AD%A5%E5%8C%B9%E9%85%8D%E5%A4%9A%E5%B9%85%E7%82%B9%E4%BA%91/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css"><script>
    (() => {
      
    const saveToLocal = {
      set: (key, value, ttl) => {
        if (!ttl) return
        const expiry = Date.now() + ttl * 86400000
        localStorage.setItem(key, JSON.stringify({ value, expiry }))
      },
      get: key => {
        const itemStr = localStorage.getItem(key)
        if (!itemStr) return undefined
        const { value, expiry } = JSON.parse(itemStr)
        if (Date.now() > expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return value
      }
    }

    window.btf = {
      saveToLocal,
      getScript: (url, attr = {}) => new Promise((resolve, reject) => {
        const script = document.createElement('script')
        script.src = url
        script.async = true
        Object.entries(attr).forEach(([key, val]) => script.setAttribute(key, val))
        script.onload = script.onreadystatechange = () => {
          if (!script.readyState || /loaded|complete/.test(script.readyState)) resolve()
        }
        script.onerror = reject
        document.head.appendChild(script)
      }),
      getCSS: (url, id) => new Promise((resolve, reject) => {
        const link = document.createElement('link')
        link.rel = 'stylesheet'
        link.href = url
        if (id) link.id = id
        link.onload = link.onreadystatechange = () => {
          if (!link.readyState || /loaded|complete/.test(link.readyState)) resolve()
        }
        link.onerror = reject
        document.head.appendChild(link)
      }),
      addGlobalFn: (key, fn, name = false, parent = window) => {
        if (!false && key.startsWith('pjax')) return
        const globalFn = parent.globalFn || {}
        globalFn[key] = globalFn[key] || {}
        globalFn[key][name || Object.keys(globalFn[key]).length] = fn
        parent.globalFn = globalFn
      }
    }
  
      
      const activateDarkMode = () => {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      const activateLightMode = () => {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }

      btf.activateDarkMode = activateDarkMode
      btf.activateLightMode = activateLightMode

      const theme = saveToLocal.get('theme')
    
          theme === 'dark' ? activateDarkMode() : theme === 'light' ? activateLightMode() : null
        
      
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        document.documentElement.classList.toggle('hide-aside', asideStatus === 'hide')
      }
    
      
    const detectApple = () => {
      if (/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)) {
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
  
    })()
  </script><script>const GLOBAL_CONFIG = {
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":true,"top_n_per_article":1,"unescape":false,"languages":{"hits_empty":"未找到符合您查询的内容：${query}","hits_stats":"共找到 ${hits} 篇文章"}},
  translate: undefined,
  highlight: {"plugin":"highlight.js","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false,"highlightFullpage":false,"highlightMacStyle":true},
  copy: {
    success: '复制成功',
    error: '复制失败',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'null',
  Snackbar: undefined,
  infinitegrid: {
    js: 'https://cdn.jsdelivr.net/npm/@egjs/infinitegrid/dist/infinitegrid.min.js',
    buttonText: '加载更多'
  },
  isPhotoFigcaption: false,
  islazyloadPlugin: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '逐步匹配多幅点云',
  isHighlightShrink: false,
  isToc: true,
  pageType: 'post'
}</script><link rel="stylesheet" href="/css/custom.css"><link rel="stylesheet" href="/css/pig.css"><link rel="stylesheet" href="https://npm.elemecdn.com/ethan4116-blog/lib/css/plane_v2.css"><meta name="generator" content="Hexo 7.3.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img text-center"><img src="/image/avatar.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data text-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">54</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">15</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">8</div></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/booklist/"><i class="fa-fw fas fa-book"></i><span> 书单</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 链接</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url(/image/per%20page.jpg);"><nav id="nav"><span id="blog-info"><a class="nav-site-title" href="/"><span class="site-name">荒岛</span></a><a class="nav-page-title" href="/"><span class="site-name">逐步匹配多幅点云</span><span class="site-name"><i class="fa-solid fa-circle-arrow-left"></i><span>  返回首页</span></span></a></span><div id="menus"><div id="search-button"><span class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></span></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/booklist/"><i class="fa-fw fas fa-book"></i><span> 书单</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 链接</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><span class="site-page"><i class="fas fa-bars fa-fw"></i></span></div></div></nav><div id="post-info"><h1 class="post-title">逐步匹配多幅点云</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2025-08-04T01:56:31.000Z" title="发表于 2025-08-04 09:56:31">2025-08-04</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2025-08-04T02:05:10.507Z" title="更新于 2025-08-04 10:05:10">2025-08-04</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/PCL/">PCL</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title=""><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">浏览量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="container post-content" id="article-container"><h1 id="PCL-点云配准"><a href="#PCL-点云配准" class="headerlink" title="PCL 点云配准"></a>PCL 点云配准</h1><h2 id="📌-1-核心目标"><a href="#📌-1-核心目标" class="headerlink" title="📌 1. 核心目标"></a>📌 1. 核心目标</h2><p>实现 <strong>增量式点云配准（Pairwise Incremental Registration）</strong>，将多个 <code>.pcd</code> 点云依次配准并拼接成一个完整模型。</p>
<hr>
<h2 id="📌-2-主要流程"><a href="#📌-2-主要流程" class="headerlink" title="📌 2. 主要流程"></a>📌 2. 主要流程</h2><table>
<thead>
<tr>
<th>步骤</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>① 加载数据</td>
<td>从命令行读取多个 <code>.pcd</code> 文件，存入 <code>std::vector&lt;PCD&gt;</code></td>
</tr>
<tr>
<td>② 预处理</td>
<td>下采样（VoxelGrid）+ 去除 NaN 点</td>
</tr>
<tr>
<td>③ 法线估计</td>
<td>使用 <code>NormalEstimation</code> 计算每个点的法线和曲率</td>
</tr>
<tr>
<td>④ 自定义特征</td>
<td>定义 <code>MyPointRepresentation</code>，融合 <code>(x, y, z, curvature)</code> 作为匹配特征</td>
</tr>
<tr>
<td>⑤ ICP 配准</td>
<td>使用 <code>IterativeClosestPointNonLinear</code> 进行非线性配准</td>
</tr>
<tr>
<td>⑥ 可视化</td>
<td>双视口：左为原始点云，右为带曲率着色的配准过程</td>
</tr>
<tr>
<td>⑦ 增量合并</td>
<td>每次配准后更新全局变换，并将结果保存为新的 <code>.pcd</code> 文件</td>
</tr>
</tbody></table>
<hr>
<h2 id="📌-3-核心技术点"><a href="#📌-3-核心技术点" class="headerlink" title="📌 3. 核心技术点"></a>📌 3. 核心技术点</h2><table>
<thead>
<tr>
<th>技术</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td><strong>VoxelGrid 下采样</strong></td>
<td>减少点数量，提升计算效率，防止过拟合</td>
</tr>
<tr>
<td><strong>法线与曲率估计</strong></td>
<td>提供几何信息，增强配准鲁棒性</td>
</tr>
<tr>
<td><strong>自定义 PointRepresentation</strong></td>
<td>将曲率纳入距离度量，影响最近点搜索</td>
</tr>
<tr>
<td><strong>ICP-NL (Non-Linear)</strong></td>
<td>基于 Levenberg-Marquardt 优化，支持法线一致性约束</td>
</tr>
<tr>
<td><strong>动态调整 maxCorrespondenceDistance</strong></td>
<td>收敛时逐步缩小搜索范围，提高精度</td>
</tr>
<tr>
<td><strong>累积变换矩阵</strong></td>
<td><code>Ti = T_current * Ti</code> 实现迭代变换累积</td>
</tr>
<tr>
<td><strong>全局坐标变换</strong></td>
<td>所有点云最终对齐到第一个点云的坐标系</td>
</tr>
</tbody></table>
<hr>
<h2 id="📌-4-可视化设计"><a href="#📌-4-可视化设计" class="headerlink" title="📌 4. 可视化设计"></a>📌 4. 可视化设计</h2><ul>
<li><strong>双视口布局</strong>：<ul>
<li><code>vp_1</code>（左）：显示原始点云（红：源，绿：目标）</li>
<li><code>vp_2</code>（右）：显示带曲率颜色映射的配准过程</li>
</ul>
</li>
<li><strong>交互控制</strong>：<ul>
<li><code>p-&gt;spin()</code>：阻塞等待用户按 <code>q</code> 继续</li>
<li><code>p-&gt;spinOnce()</code>：非阻塞刷新，用于实时显示迭代过程</li>
</ul>
</li>
</ul>
<hr>
<h2 id="📌-5-增量配准逻辑"><a href="#📌-5-增量配准逻辑" class="headerlink" title="📌 5. 增量配准逻辑"></a>📌 5. 增量配准逻辑</h2><figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">bun0 → bun4 → bun5 → ...</span><br><span class="line">  ↓     ↓      ↓</span><br><span class="line">Step1: bun0 + bun4 → output1.pcd</span><br><span class="line">Step2: output1 + bun5 → output2.pcd</span><br><span class="line">...</span><br><span class="line">所有结果统一变换到 bun0 的坐标系下</span><br></pre></td></tr></table></figure>

<hr>
<h2 id="📌-6-参数调优建议"><a href="#📌-6-参数调优建议" class="headerlink" title="📌 6. 参数调优建议"></a>📌 6. 参数调优建议</h2><table>
<thead>
<tr>
<th>参数</th>
<th>建议值</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td><code>setLeafSize(0.05)</code></td>
<td>根据点云密度调整</td>
<td>太小会保留噪声，太大丢失细节</td>
</tr>
<tr>
<td><code>setKSearch(30)</code></td>
<td>20~50</td>
<td>邻域大小影响法线估计质量</td>
</tr>
<tr>
<td><code>setMaxCorrespondenceDistance(0.1)</code></td>
<td>初始值 10cm</td>
<td>应略大于点间距</td>
</tr>
<tr>
<td><code>setTransformationEpsilon(1e-6)</code></td>
<td>收敛阈值</td>
<td>控制迭代终止条件</td>
</tr>
</tbody></table>
<hr>
<h2 id="📌-7-常见问题与改进方向"><a href="#📌-7-常见问题与改进方向" class="headerlink" title="📌 7. 常见问题与改进方向"></a>📌 7. 常见问题与改进方向</h2><table>
<thead>
<tr>
<th>问题</th>
<th>解决方案</th>
</tr>
</thead>
<tbody><tr>
<td>初始位置偏差大导致配准失败</td>
<td>先粗配准（如 FPFH + SAC-IA）</td>
</tr>
<tr>
<td>曲率权重不合理</td>
<td>调整 <code>alpha[]</code> 权重或归一化</td>
</tr>
<tr>
<td>多文件排序混乱</td>
<td>使用 <code>PCDComparator</code> 排序</td>
</tr>
<tr>
<td>内存泄漏风险</td>
<td>避免全局 <code>new</code>，可用智能指针管理 <code>PCLVisualizer</code></td>
</tr>
</tbody></table>
<hr>
<h2 id="代码实现"><a href="#代码实现" class="headerlink" title="代码实现"></a>代码实现</h2><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* \author Radu Bogdan Rusu</span></span><br><span class="line"><span class="comment"> * adaptation Raphael Favier*/</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;boost/make_shared.hpp&gt;</span>                                <span class="comment">// 使用 boost::make_shared 创建共享指针</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;pcl/point_types.h&gt;</span>                                   <span class="comment">// PCL 基本点类型（如 PointXYZ）</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;pcl/point_cloud.h&gt;</span>                                   <span class="comment">// 点云容器模板 PointCloud&lt;T&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;pcl/point_representation.h&gt;</span>                          <span class="comment">// 自定义点特征表示方式</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;pcl/io/pcd_io.h&gt;</span>                                     <span class="comment">// 读写 .pcd 文件</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;pcl/filters/voxel_grid.h&gt;</span>                            <span class="comment">// 体素格滤波器，用于下采样</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;pcl/filters/filter.h&gt;</span>                                <span class="comment">// 提供 removeNaN 等通用滤波功能</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;pcl/features/normal_3d.h&gt;</span>                            <span class="comment">// 法线和曲率估计</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;pcl/registration/icp.h&gt;</span>                              <span class="comment">// 标准 ICP 配准算法</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;pcl/registration/icp_nl.h&gt;</span>                           <span class="comment">// 非线性 ICP（支持法线优化）</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;pcl/registration/transforms.h&gt;</span>                       <span class="comment">// 点云变换函数 transformPointCloud</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;pcl/visualization/pcl_visualizer.h&gt;</span>                  <span class="comment">// 可视化工具 PCLVisualizer</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> pcl::visualization::PointCloudColorHandlerGenericField;  <span class="comment">// 按字段着色（如 curvature）</span></span><br><span class="line"><span class="keyword">using</span> pcl::visualization::PointCloudColorHandlerCustom;        <span class="comment">// 自定义 RGB 颜色</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 简单类型定义</span></span><br><span class="line"><span class="keyword">typedef</span> pcl::PointXYZ PointT;                                  <span class="comment">// 定义基础点类型：x, y, z</span></span><br><span class="line"><span class="keyword">typedef</span> pcl::PointCloud&lt;PointT&gt; PointCloud;                    <span class="comment">// 点云类型别名</span></span><br><span class="line"><span class="keyword">typedef</span> pcl::PointNormal PointNormalT;                         <span class="comment">// 包含法线和曲率的点类型</span></span><br><span class="line"><span class="keyword">typedef</span> pcl::PointCloud&lt;PointNormalT&gt; PointCloudWithNormals;   <span class="comment">// 带法线信息的点云类型</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 这是一个辅助教程，因此我们可以负担全局变量</span></span><br><span class="line">pcl::visualization::PCLVisualizer *p;                         <span class="comment">// 全局可视化器指针</span></span><br><span class="line"><span class="type">int</span> vp_1, vp_2;                                               <span class="comment">// 定义两个视口 ID：左(vp_1) 和右(vp_2)</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 处理点云的方便结构体</span></span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">PCD</span></span><br><span class="line">&#123;</span><br><span class="line">  PointCloud::Ptr cloud;                                       <span class="comment">// 存储点云数据</span></span><br><span class="line">  std::string f_name;                                          <span class="comment">// 文件名</span></span><br><span class="line">  <span class="built_in">PCD</span>() : <span class="built_in">cloud</span>(<span class="keyword">new</span> PointCloud) &#123;&#125;;                           <span class="comment">// 构造函数：初始化空点云</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">PCDComparator</span>                                <span class="comment">// 用于排序 PCD 的比较器（按文件名）</span></span><br><span class="line">&#123;</span><br><span class="line">  <span class="function"><span class="type">bool</span> <span class="title">operator</span><span class="params">()</span><span class="params">(<span class="type">const</span> PCD&amp; p1, <span class="type">const</span> PCD&amp; p2)</span></span></span><br><span class="line"><span class="function">  </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> (p<span class="number">1.f</span>_name &lt; p<span class="number">2.f</span>_name);                 <span class="comment">// 字符串比较：升序排列</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 自定义点表示：&lt;x, y, z, curvature&gt;</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">MyPointRepresentation</span> : <span class="keyword">public</span> pcl::PointRepresentation&lt;PointNormalT&gt;</span><br><span class="line">&#123;</span><br><span class="line">  <span class="keyword">using</span> pcl::PointRepresentation&lt;PointNormalT&gt;::nr_dimensions_; <span class="comment">// 继承维度变量</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">  <span class="built_in">MyPointRepresentation</span>()</span><br><span class="line">  &#123;</span><br><span class="line">    nr_dimensions_ = <span class="number">4</span>;                             <span class="comment">// 设置特征维度为 4</span></span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">virtual</span> <span class="type">void</span> <span class="title">copyToFloatArray</span><span class="params">(<span class="type">const</span> PointNormalT &amp;p, <span class="type">float</span> *out)</span> <span class="type">const</span></span></span><br><span class="line"><span class="function">  </span>&#123;</span><br><span class="line">    out[<span class="number">0</span>] = p.x;                                   <span class="comment">// x 坐标</span></span><br><span class="line">    out[<span class="number">1</span>] = p.y;                                   <span class="comment">// y 坐标</span></span><br><span class="line">    out[<span class="number">2</span>] = p.z;                                   <span class="comment">// z 坐标</span></span><br><span class="line">    out[<span class="number">3</span>] = p.curvature;                           <span class="comment">// 曲率作为第四个特征</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">////////////////////////////////////////////////////////////////////////////////</span></span><br><span class="line"><span class="comment">/** 在左侧视口显示原始源点云和目标点云（无颜色映射） */</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">showCloudsLeft</span><span class="params">(<span class="type">const</span> PointCloud::Ptr cloud_target, <span class="type">const</span> PointCloud::Ptr cloud_source)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">  p-&gt;<span class="built_in">removePointCloud</span>(<span class="string">&quot;vp1_target&quot;</span>);                <span class="comment">// 清除旧目标点云</span></span><br><span class="line">  p-&gt;<span class="built_in">removePointCloud</span>(<span class="string">&quot;vp1_source&quot;</span>);                <span class="comment">// 清除旧源点云</span></span><br><span class="line">  <span class="function">PointCloudColorHandlerCustom&lt;PointT&gt; <span class="title">tgt_h</span><span class="params">(cloud_target, <span class="number">0</span>, <span class="number">255</span>, <span class="number">0</span>)</span></span>; <span class="comment">// 绿色显示目标</span></span><br><span class="line">  <span class="function">PointCloudColorHandlerCustom&lt;PointT&gt; <span class="title">src_h</span><span class="params">(cloud_source, <span class="number">255</span>, <span class="number">0</span>, <span class="number">0</span>)</span></span>; <span class="comment">// 红色显示源</span></span><br><span class="line">  p-&gt;<span class="built_in">addPointCloud</span>(cloud_target, tgt_h, <span class="string">&quot;vp1_target&quot;</span>, vp_1); <span class="comment">// 添加到左视口</span></span><br><span class="line">  p-&gt;<span class="built_in">addPointCloud</span>(cloud_source, src_h, <span class="string">&quot;vp1_source&quot;</span>, vp_1);</span><br><span class="line">  <span class="built_in">PCL_INFO</span>(<span class="string">&quot;Press q to begin the registration.\n&quot;</span>); <span class="comment">// 提示用户按 q 开始配准</span></span><br><span class="line">  p-&gt;<span class="built_in">spin</span>();                                        <span class="comment">// 进入可视化循环，等待按键</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">////////////////////////////////////////////////////////////////////////////////</span></span><br><span class="line"><span class="comment">/** 在右侧视口显示带法线/曲率的点云（彩色映射曲率） */</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">showCloudsRight</span><span class="params">(<span class="type">const</span> PointCloudWithNormals::Ptr cloud_target, <span class="type">const</span> PointCloudWithNormals::Ptr cloud_source)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">  p-&gt;<span class="built_in">removePointCloud</span>(<span class="string">&quot;source&quot;</span>);                    <span class="comment">// 清除之前点云</span></span><br><span class="line">  p-&gt;<span class="built_in">removePointCloud</span>(<span class="string">&quot;target&quot;</span>);</span><br><span class="line">  <span class="function">PointCloudColorHandlerGenericField&lt;PointNormalT&gt; <span class="title">tgt_color_handler</span><span class="params">(cloud_target, <span class="string">&quot;curvature&quot;</span>)</span></span>; <span class="comment">// 按曲率着色</span></span><br><span class="line">  <span class="keyword">if</span> (!tgt_color_handler.<span class="built_in">isCapable</span>())</span><br><span class="line">      <span class="built_in">PCL_WARN</span>(<span class="string">&quot;Cannot create curvature color handler!&quot;</span>); <span class="comment">// 检查是否支持</span></span><br><span class="line">  <span class="function">PointCloudColorHandlerGenericField&lt;PointNormalT&gt; <span class="title">src_color_handler</span><span class="params">(cloud_source, <span class="string">&quot;curvature&quot;</span>)</span></span>;</span><br><span class="line">  <span class="keyword">if</span> (!src_color_handler.<span class="built_in">isCapable</span>())</span><br><span class="line">      <span class="built_in">PCL_WARN</span>(<span class="string">&quot;Cannot create curvature color handler!&quot;</span>);</span><br><span class="line">  p-&gt;<span class="built_in">addPointCloud</span>(cloud_target, tgt_color_handler, <span class="string">&quot;target&quot;</span>, vp_2); <span class="comment">// 添加到右视口</span></span><br><span class="line">  p-&gt;<span class="built_in">addPointCloud</span>(cloud_source, src_color_handler, <span class="string">&quot;source&quot;</span>, vp_2);</span><br><span class="line">  p-&gt;<span class="built_in">spinOnce</span>();                                    <span class="comment">// 单次刷新，不阻塞</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">////////////////////////////////////////////////////////////////////////////////</span></span><br><span class="line"><span class="comment">/** 加载命令行传入的所有 .pcd 文件 */</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">loadData</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> **argv, std::vector&lt;PCD, Eigen::aligned_allocator&lt;PCD&gt;&gt; &amp;models)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">  <span class="function">std::string <span class="title">extension</span><span class="params">(<span class="string">&quot;.pcd&quot;</span>)</span></span>;                    <span class="comment">// 只加载 .pcd 文件</span></span><br><span class="line">  <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">1</span>; i &lt; argc; i++)                    <span class="comment">// 遍历所有参数（跳过程序名）</span></span><br><span class="line">  &#123;</span><br><span class="line">    std::string fname = std::<span class="built_in">string</span>(argv[i]);</span><br><span class="line">    <span class="keyword">if</span> (fname.<span class="built_in">size</span>() &lt;= extension.<span class="built_in">size</span>())           <span class="comment">// 忽略太短的文件名</span></span><br><span class="line">      <span class="keyword">continue</span>;</span><br><span class="line">    std::<span class="built_in">transform</span>(fname.<span class="built_in">begin</span>(), fname.<span class="built_in">end</span>(), fname.<span class="built_in">begin</span>(), (<span class="built_in">int</span>(*)(<span class="type">int</span>))tolower); <span class="comment">// 转小写</span></span><br><span class="line">    <span class="keyword">if</span> (fname.<span class="built_in">compare</span>(fname.<span class="built_in">size</span>() - extension.<span class="built_in">size</span>(), extension.<span class="built_in">size</span>(), extension) == <span class="number">0</span>) <span class="comment">// 检查后缀</span></span><br><span class="line">    &#123;</span><br><span class="line">      PCD m;</span><br><span class="line">      m.f_name = argv[i];</span><br><span class="line">      pcl::io::<span class="built_in">loadPCDFile</span>(argv[i], *m.cloud);      <span class="comment">// 读取点云</span></span><br><span class="line">      std::vector&lt;<span class="type">int</span>&gt; indices;</span><br><span class="line">      pcl::<span class="built_in">removeNaNFromPointCloud</span>(*m.cloud, *m.cloud, indices); <span class="comment">// 移除 NaN 点</span></span><br><span class="line">      models.<span class="built_in">push_back</span>(m);                          <span class="comment">// 加入数据集</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">////////////////////////////////////////////////////////////////////////////////</span></span><br><span class="line"><span class="comment">/** 执行两两点云配准（ICP-NL + 法线 + 曲率） */</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">pairAlign</span><span class="params">(<span class="type">const</span> PointCloud::Ptr cloud_src, <span class="type">const</span> PointCloud::Ptr cloud_tgt,</span></span></span><br><span class="line"><span class="params"><span class="function">               PointCloud::Ptr output, Eigen::Matrix4f &amp;final_transform, <span class="type">bool</span> downsample = <span class="literal">false</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">  <span class="function">PointCloud::Ptr <span class="title">src</span><span class="params">(<span class="keyword">new</span> PointCloud)</span></span>;</span><br><span class="line">  <span class="function">PointCloud::Ptr <span class="title">tgt</span><span class="params">(<span class="keyword">new</span> PointCloud)</span></span>;</span><br><span class="line">  pcl::VoxelGrid&lt;PointT&gt; grid;</span><br><span class="line">  <span class="keyword">if</span> (downsample)</span><br><span class="line">  &#123;</span><br><span class="line">    grid.<span class="built_in">setLeafSize</span>(<span class="number">0.05</span>, <span class="number">0.05</span>, <span class="number">0.05</span>);             <span class="comment">// 设置体素大小为 5cm</span></span><br><span class="line">    grid.<span class="built_in">setInputCloud</span>(cloud_src);</span><br><span class="line">    grid.<span class="built_in">filter</span>(*src);</span><br><span class="line">    grid.<span class="built_in">setInputCloud</span>(cloud_tgt);</span><br><span class="line">    grid.<span class="built_in">filter</span>(*tgt);</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">else</span></span><br><span class="line">  &#123;</span><br><span class="line">    src = cloud_src;</span><br><span class="line">    tgt = cloud_tgt;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 计算法线和曲率</span></span><br><span class="line">  <span class="function">PointCloudWithNormals::Ptr <span class="title">points_with_normals_src</span><span class="params">(<span class="keyword">new</span> PointCloudWithNormals)</span></span>;</span><br><span class="line">  <span class="function">PointCloudWithNormals::Ptr <span class="title">points_with_normals_tgt</span><span class="params">(<span class="keyword">new</span> PointCloudWithNormals)</span></span>;</span><br><span class="line">  pcl::NormalEstimation&lt;PointT, PointNormalT&gt; norm_est;</span><br><span class="line">  pcl::search::KdTree&lt;pcl::PointXYZ&gt;::<span class="function">Ptr <span class="title">tree</span><span class="params">(<span class="keyword">new</span> pcl::search::KdTree&lt;pcl::PointXYZ&gt;())</span></span>;</span><br><span class="line">  norm_est.<span class="built_in">setSearchMethod</span>(tree);</span><br><span class="line">  norm_est.<span class="built_in">setKSearch</span>(<span class="number">30</span>);                          <span class="comment">// 使用 K 近邻搜索（30个邻居）</span></span><br><span class="line">  norm_est.<span class="built_in">setInputCloud</span>(src);</span><br><span class="line">  norm_est.<span class="built_in">compute</span>(*points_with_normals_src);</span><br><span class="line">  pcl::<span class="built_in">copyPointCloud</span>(*src, *points_with_normals_src); <span class="comment">// 将坐标复制到带法线的结构中</span></span><br><span class="line">  norm_est.<span class="built_in">setInputCloud</span>(tgt);</span><br><span class="line">  norm_est.<span class="built_in">compute</span>(*points_with_normals_tgt);</span><br><span class="line">  pcl::<span class="built_in">copyPointCloud</span>(*tgt, *points_with_normals_tgt);</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 使用自定义点表示（x, y, z, curvature）</span></span><br><span class="line">  MyPointRepresentation point_representation;</span><br><span class="line">  <span class="type">float</span> alpha[<span class="number">4</span>] = &#123;<span class="number">1.0</span>, <span class="number">1.0</span>, <span class="number">1.0</span>, <span class="number">1.0</span>&#125;;</span><br><span class="line">  point_representation.<span class="built_in">setRescaleValues</span>(alpha);     <span class="comment">// 设置各维度权重相同</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">// ICP 非线性配准</span></span><br><span class="line">  pcl::IterativeClosestPointNonLinear&lt;PointNormalT, PointNormalT&gt; reg;</span><br><span class="line">  reg.<span class="built_in">setTransformationEpsilon</span>(<span class="number">1e-6</span>);               <span class="comment">// 收敛阈值：变换增量极小则停止</span></span><br><span class="line">  reg.<span class="built_in">setMaxCorrespondenceDistance</span>(<span class="number">0.1</span>);            <span class="comment">// 最大对应点距离为 10cm</span></span><br><span class="line">  reg.<span class="built_in">setPointRepresentation</span>(boost::<span class="built_in">make_shared</span>&lt;<span class="type">const</span> MyPointRepresentation&gt;(point_representation));</span><br><span class="line">  reg.<span class="built_in">setInputCloud</span>(points_with_normals_src);</span><br><span class="line">  reg.<span class="built_in">setInputTarget</span>(points_with_normals_tgt);</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 迭代优化并可视化</span></span><br><span class="line">  Eigen::Matrix4f Ti = Eigen::Matrix4f::<span class="built_in">Identity</span>(), prev, targetToSource;</span><br><span class="line">  PointCloudWithNormals::Ptr reg_result = points_with_normals_src;</span><br><span class="line">  reg.<span class="built_in">setMaximumIterations</span>(<span class="number">2</span>);                      <span class="comment">// 每轮最多迭代 2 次（循环外控制总次数）</span></span><br><span class="line"></span><br><span class="line">  <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; <span class="number">30</span>; ++i)</span><br><span class="line">  &#123;</span><br><span class="line">    <span class="built_in">PCL_INFO</span>(<span class="string">&quot;Iteration Nr. %d.\n&quot;</span>, i);</span><br><span class="line">    points_with_normals_src = reg_result;           <span class="comment">// 更新输入为上次结果</span></span><br><span class="line">    reg.<span class="built_in">setInputCloud</span>(points_with_normals_src);</span><br><span class="line">    reg.<span class="built_in">align</span>(*reg_result);                         <span class="comment">// 执行一次 ICP 对齐</span></span><br><span class="line"></span><br><span class="line">    Ti = reg.<span class="built_in">getFinalTransformation</span>() * Ti;         <span class="comment">// 累积变换矩阵</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 若变换增量很小，缩小最大对应距离以提升精度</span></span><br><span class="line">    <span class="keyword">if</span> (<span class="built_in">fabs</span>((reg.<span class="built_in">getLastIncrementalTransformation</span>() - prev).<span class="built_in">sum</span>()) &lt; reg.<span class="built_in">getTransformationEpsilon</span>())</span><br><span class="line">      reg.<span class="built_in">setMaxCorrespondenceDistance</span>(reg.<span class="built_in">getMaxCorrespondenceDistance</span>() - <span class="number">0.001</span>);</span><br><span class="line">    prev = reg.<span class="built_in">getLastIncrementalTransformation</span>();</span><br><span class="line"></span><br><span class="line">    <span class="built_in">showCloudsRight</span>(points_with_normals_tgt, points_with_normals_src); <span class="comment">// 实时可视化</span></span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  targetToSource = Ti.<span class="built_in">inverse</span>();                    <span class="comment">// 得到从目标到源的变换</span></span><br><span class="line">  pcl::<span class="built_in">transformPointCloud</span>(*cloud_tgt, *output, targetToSource); <span class="comment">// 将目标点云变换到源坐标系</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">// 可视化最终配准结果</span></span><br><span class="line">  p-&gt;<span class="built_in">removePointCloud</span>(<span class="string">&quot;source&quot;</span>);</span><br><span class="line">  p-&gt;<span class="built_in">removePointCloud</span>(<span class="string">&quot;target&quot;</span>);</span><br><span class="line">  <span class="function">PointCloudColorHandlerCustom&lt;PointT&gt; <span class="title">cloud_tgt_h</span><span class="params">(output, <span class="number">0</span>, <span class="number">255</span>, <span class="number">0</span>)</span></span>; <span class="comment">// 绿色为目标</span></span><br><span class="line">  <span class="function">PointCloudColorHandlerCustom&lt;PointT&gt; <span class="title">cloud_src_h</span><span class="params">(cloud_src, <span class="number">255</span>, <span class="number">0</span>, <span class="number">0</span>)</span></span>; <span class="comment">// 红色为源</span></span><br><span class="line">  p-&gt;<span class="built_in">addPointCloud</span>(output, cloud_tgt_h, <span class="string">&quot;target&quot;</span>, vp_2);</span><br><span class="line">  p-&gt;<span class="built_in">addPointCloud</span>(cloud_src, cloud_src_h, <span class="string">&quot;source&quot;</span>, vp_2);</span><br><span class="line">  <span class="built_in">PCL_INFO</span>(<span class="string">&quot;Press q to continue the registration.\n&quot;</span>);</span><br><span class="line">  p-&gt;<span class="built_in">spin</span>();</span><br><span class="line">  p-&gt;<span class="built_in">removePointCloud</span>(<span class="string">&quot;source&quot;</span>);</span><br><span class="line">  p-&gt;<span class="built_in">removePointCloud</span>(<span class="string">&quot;target&quot;</span>);</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 合并源点云与配准后的目标点云</span></span><br><span class="line">  *output += *cloud_src;</span><br><span class="line">  final_transform = targetToSource;                 <span class="comment">// 返回最终变换矩阵</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/* ---[ */</span></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span>** argv)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">  std::vector&lt;PCD, Eigen::aligned_allocator&lt;PCD&gt;&gt; data;</span><br><span class="line">  <span class="built_in">loadData</span>(argc, argv, data);                       <span class="comment">// 加载所有 .pcd 文件</span></span><br><span class="line"></span><br><span class="line">  <span class="keyword">if</span> (data.<span class="built_in">empty</span>())</span><br><span class="line">  &#123;</span><br><span class="line">    <span class="built_in">PCL_ERROR</span>(<span class="string">&quot;Syntax is: %s &lt;source.pcd&gt; &lt;target.pcd&gt; [*]\n&quot;</span>, argv[<span class="number">0</span>]);</span><br><span class="line">    <span class="built_in">PCL_ERROR</span>(<span class="string">&quot;[*] - multiple files can be added. The registration results of (i, i+1) will be registered against (i+2), etc\n&quot;</span>);</span><br><span class="line">    <span class="built_in">PCL_INFO</span>(<span class="string">&quot;Example: %s `rospack find pcl`/test/bun0.pcd `rospack find pcl`/test/bun4.pcd\n&quot;</span>, argv[<span class="number">0</span>]);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="built_in">PCL_INFO</span>(<span class="string">&quot;Loaded %d datasets.\n&quot;</span>, (<span class="type">int</span>)data.<span class="built_in">size</span>());</span><br><span class="line"></span><br><span class="line">  p = <span class="keyword">new</span> pcl::visualization::<span class="built_in">PCLVisualizer</span>(argc, argv, <span class="string">&quot;Pairwise Incremental Registration example&quot;</span>);</span><br><span class="line">  p-&gt;<span class="built_in">createViewPort</span>(<span class="number">0.0</span>, <span class="number">0</span>, <span class="number">0.5</span>, <span class="number">1.0</span>, vp_1);        <span class="comment">// 左视口：原始点云</span></span><br><span class="line">  p-&gt;<span class="built_in">createViewPort</span>(<span class="number">0.5</span>, <span class="number">0</span>, <span class="number">1.0</span>, <span class="number">1.0</span>, vp_2);         <span class="comment">// 右视口：带曲率着色的配准过程</span></span><br><span class="line"></span><br><span class="line">  <span class="function">PointCloud::Ptr <span class="title">result</span><span class="params">(<span class="keyword">new</span> PointCloud)</span>, source, target</span>;</span><br><span class="line">  Eigen::Matrix4f GlobalTransform = Eigen::Matrix4f::<span class="built_in">Identity</span>(), pairTransform;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">for</span> (<span class="type">size_t</span> i = <span class="number">1</span>; i &lt; data.<span class="built_in">size</span>(); ++i)</span><br><span class="line">  &#123;</span><br><span class="line">    source = data[i<span class="number">-1</span>].cloud;                       <span class="comment">// 当前源：前一个点云</span></span><br><span class="line">    target = data[i].cloud;                         <span class="comment">// 当前目标：当前点云</span></span><br><span class="line"></span><br><span class="line">    <span class="built_in">showCloudsLeft</span>(source, target);                 <span class="comment">// 显示原始配对</span></span><br><span class="line">    <span class="function">PointCloud::Ptr <span class="title">temp</span><span class="params">(<span class="keyword">new</span> PointCloud)</span></span>;</span><br><span class="line">    <span class="built_in">PCL_INFO</span>(<span class="string">&quot;Aligning %s (%d) with %s (%d).\n&quot;</span>,</span><br><span class="line">             data[i<span class="number">-1</span>].f_name.<span class="built_in">c_str</span>(), (<span class="type">int</span>)source-&gt;points.<span class="built_in">size</span>(),</span><br><span class="line">             data[i].f_name.<span class="built_in">c_str</span>(), (<span class="type">int</span>)target-&gt;points.<span class="built_in">size</span>());</span><br><span class="line"></span><br><span class="line">    <span class="built_in">pairAlign</span>(source, target, temp, pairTransform, <span class="literal">true</span>); <span class="comment">// 执行配准</span></span><br><span class="line"></span><br><span class="line">    pcl::<span class="built_in">transformPointCloud</span>(*temp, *result, GlobalTransform); <span class="comment">// 应用全局变换</span></span><br><span class="line">    GlobalTransform = pairTransform * GlobalTransform; <span class="comment">// 更新全局变换</span></span><br><span class="line"></span><br><span class="line">    std::stringstream ss;</span><br><span class="line">    ss &lt;&lt; i &lt;&lt; <span class="string">&quot;.pcd&quot;</span>;</span><br><span class="line">    pcl::io::<span class="built_in">savePCDFile</span>(ss.<span class="built_in">str</span>(), *result, <span class="literal">true</span>);   <span class="comment">// 保存第 i 步配准结果</span></span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/* ]--- */</span></span><br></pre></td></tr></table></figure>



<hr>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta"><i class="fas fa-circle-user fa-fw"></i>文章作者: </span><span class="post-copyright-info"><a href="https://ckyfi9zero.github.io">Fi9zero</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta"><i class="fas fa-square-arrow-up-right fa-fw"></i>文章链接: </span><span class="post-copyright-info"><a href="https://ckyfi9zero.github.io/2025/08/04/2025-08-04-%E9%80%90%E6%AD%A5%E5%8C%B9%E9%85%8D%E5%A4%9A%E5%B9%85%E7%82%B9%E4%BA%91/">https://ckyfi9zero.github.io/2025/08/04/2025-08-04-%E9%80%90%E6%AD%A5%E5%8C%B9%E9%85%8D%E5%A4%9A%E5%B9%85%E7%82%B9%E4%BA%91/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta"><i class="fas fa-circle-exclamation fa-fw"></i>版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来源 <a href="https://ckyfi9zero.github.io" target="_blank">荒岛</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/c/">c++</a><a class="post-meta__tags" href="/tags/PCL/">PCL</a></div><div class="post-share"><div class="social-share" data-image="/image/avatar.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><a class="pagination-related  no-desc" href="/2025/08/04/2025-08-04-%E6%AD%A3%E6%80%81%E5%88%86%E5%B8%83%E5%8F%98%E6%8D%A2%E9%85%8D%E5%87%86/" title="正态分布变换配准"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info"><div class="info-1"><div class="info-item-1">上一篇</div><div class="info-item-2">正态分布变换配准</div></div></div></a><a class="pagination-related  no-desc" href="/2025/08/03/2025-08-03-ICP%E8%BF%AD%E4%BB%A3%E6%9C%80%E8%BF%91%E7%82%B9%E7%AE%97%E6%B3%95/" title="ICP迭代最近点算法"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-right"><div class="info-1"><div class="info-item-1">下一篇</div><div class="info-item-2">ICP迭代最近点算法</div></div></div></a></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><a class="pagination-related no-desc" href="/2025/07/31/2025-07-31-%E7%94%A8kdtree%E5%AE%9E%E7%8E%B0%E5%BF%AB%E9%80%9F%E9%A2%86%E5%9F%9F%E6%90%9C%E7%B4%A2/" title="用kdtree实现快速领域搜索"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2025-07-31</div><div class="info-item-2">用kdtree实现快速领域搜索</div></div></div></a><a class="pagination-related no-desc" href="/2025/07/31/2025-07-31-%E7%94%A8%E5%85%AB%E5%8F%89%E6%A0%91%E8%BF%9B%E8%A1%8C%E6%97%A0%E5%BA%8F%E7%82%B9%E4%BA%91%E6%95%B0%E6%8D%AE%E7%9A%84%E7%A9%BA%E9%97%B4%E5%8F%98%E5%8C%96%E6%A3%80%E6%B5%8B/" title="用八叉树进行无序点云数据的空间变化检测"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2025-07-31</div><div class="info-item-2">用八叉树进行无序点云数据的空间变化检测</div></div></div></a><a class="pagination-related no-desc" href="/2025/07/31/2025-07-31-%E7%94%A8%E7%9B%B4%E9%80%9A%E6%BB%A4%E6%B3%A2%E5%99%A8%E5%AF%B9%E7%82%B9%E4%BA%91%E8%BF%9B%E8%A1%8C%E6%BB%A4%E6%B3%A2%E5%A4%84%E7%90%86/" title="用直通滤波器对点云进行滤波处理"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2025-07-31</div><div class="info-item-2">用直通滤波器对点云进行滤波处理</div></div></div></a><a class="pagination-related no-desc" href="/2025/08/01/2025-08-01-%E4%BB%8E%E7%82%B9%E4%BA%91%E5%88%9B%E5%BB%BA%E4%B8%80%E4%B8%AA%E6%B7%B1%E5%BA%A6%E5%9B%BE%E5%83%8F/" title="从点云创建一个深度图像"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2025-08-01</div><div class="info-item-2">从点云创建一个深度图像</div></div></div></a><a class="pagination-related no-desc" href="/2025/08/01/2025-08-01-%E4%BB%8E%E4%B8%80%E4%B8%AA%E7%82%B9%E4%BA%91%E4%B8%AD%E6%8F%90%E5%8F%96%E4%B8%80%E4%B8%AA%E5%AD%90%E9%9B%86/" title="从一个点云中提取一个子集"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2025-08-01</div><div class="info-item-2">从一个点云中提取一个子集</div></div></div></a><a class="pagination-related no-desc" href="/2025/08/01/2025-08-01-%E5%9C%A8%E6%BB%A4%E6%B3%A2%E5%99%A8%E6%A8%A1%E5%9D%97%E7%A7%BB%E9%99%A4%E7%A6%BB%E7%BE%A4%E7%82%B9/" title="在滤波器模块移除离群点"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2025-08-01</div><div class="info-item-2">在滤波器模块移除离群点</div></div></div></a></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info text-center"><div class="avatar-img"><img src="/image/avatar.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info-name">Fi9zero</div><div class="author-info-description"></div><div class="site-data"><a href="/archives/"><div class="headline">文章</div><div class="length-num">54</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">15</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">8</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/ckyFi9zero"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons"><a class="social-icon" href="https://github.com/ckyFi9zero" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="mailto:2539894206@qq.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#PCL-%E7%82%B9%E4%BA%91%E9%85%8D%E5%87%86"><span class="toc-number">1.</span> <span class="toc-text">PCL 点云配准</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%F0%9F%93%8C-1-%E6%A0%B8%E5%BF%83%E7%9B%AE%E6%A0%87"><span class="toc-number">1.1.</span> <span class="toc-text">📌 1. 核心目标</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%F0%9F%93%8C-2-%E4%B8%BB%E8%A6%81%E6%B5%81%E7%A8%8B"><span class="toc-number">1.2.</span> <span class="toc-text">📌 2. 主要流程</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%F0%9F%93%8C-3-%E6%A0%B8%E5%BF%83%E6%8A%80%E6%9C%AF%E7%82%B9"><span class="toc-number">1.3.</span> <span class="toc-text">📌 3. 核心技术点</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%F0%9F%93%8C-4-%E5%8F%AF%E8%A7%86%E5%8C%96%E8%AE%BE%E8%AE%A1"><span class="toc-number">1.4.</span> <span class="toc-text">📌 4. 可视化设计</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%F0%9F%93%8C-5-%E5%A2%9E%E9%87%8F%E9%85%8D%E5%87%86%E9%80%BB%E8%BE%91"><span class="toc-number">1.5.</span> <span class="toc-text">📌 5. 增量配准逻辑</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%F0%9F%93%8C-6-%E5%8F%82%E6%95%B0%E8%B0%83%E4%BC%98%E5%BB%BA%E8%AE%AE"><span class="toc-number">1.6.</span> <span class="toc-text">📌 6. 参数调优建议</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%F0%9F%93%8C-7-%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98%E4%B8%8E%E6%94%B9%E8%BF%9B%E6%96%B9%E5%90%91"><span class="toc-number">1.7.</span> <span class="toc-text">📌 7. 常见问题与改进方向</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BB%A3%E7%A0%81%E5%AE%9E%E7%8E%B0"><span class="toc-number">1.8.</span> <span class="toc-text">代码实现</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2025/10/05/2025-10-05-lio_sam/" title="lio_sam">lio_sam</a><time datetime="2025-10-05T02:43:51.000Z" title="发表于 2025-10-05 10:43:51">2025-10-05</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2025/08/12/2025-08-12-%E4%BD%BF%E7%94%A8docker%E6%90%AD%E5%BB%BApytorch%E7%8E%AF%E5%A2%83/" title="使用docker搭建pytorch环境">使用docker搭建pytorch环境</a><time datetime="2025-08-12T12:43:33.000Z" title="发表于 2025-08-12 20:43:33">2025-08-12</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2025/08/05/2025-08-05-docker/" title="docker">docker</a><time datetime="2025-08-05T11:02:36.000Z" title="发表于 2025-08-05 19:02:36">2025-08-05</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2025/08/05/2025-08-05-%E8%BF%90%E5%8A%A8%E5%AF%B9%E8%B1%A1%E5%88%86%E5%89%B2%E4%B8%8E%E9%85%8D%E5%87%86%E7%AE%97%E6%B3%95/" title="运动对象分割与配准算法">运动对象分割与配准算法</a><time datetime="2025-08-05T08:16:02.000Z" title="发表于 2025-08-05 16:16:02">2025-08-05</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2025/08/05/2025-08-05-%E6%9D%A1%E4%BB%B6%E6%AC%A7%E5%BC%8F%E8%81%9A%E7%B1%BB%E5%88%86%E5%89%B2/" title="条件欧式聚类分割">条件欧式聚类分割</a><time datetime="2025-08-05T07:56:39.000Z" title="发表于 2025-08-05 15:56:39">2025-08-05</time></div></div></div></div></div></div></main><footer id="footer"><div class="footer-other"><div class="footer-copyright"><span class="copyright">&copy;&nbsp;2025 By Fi9zero</span><span class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo 7.3.0</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly 5.4.2</a></span></div><div class="footer_custom_text">Only light can attract bugs.</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="日间和夜间模式切换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside-config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><div class="js-pjax"><script>(() => {
  const loadMathjax = () => {
    if (!window.MathJax) {
      window.MathJax = {
        tex: {
          inlineMath: [['$', '$'], ['\\(', '\\)']],
          tags: 'none',
        },
        chtml: {
          scale: 1.1
        },
        options: {
          enableMenu: true,
          renderActions: {
            findScript: [10, doc => {
              for (const node of document.querySelectorAll('script[type^="math/tex"]')) {
                const display = !!node.type.match(/; *mode=display/)
                const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display)
                const text = document.createTextNode('')
                node.parentNode.replaceChild(text, node)
                math.start = {node: text, delim: '', n: 0}
                math.end = {node: text, delim: '', n: 0}
                doc.math.push(math)
              }
            }, '']
          }
        }
      }

      const script = document.createElement('script')
      script.src = 'https://cdn.jsdelivr.net/npm/mathjax/es5/tex-mml-chtml.min.js'
      script.id = 'MathJax-script'
      script.async = true
      document.head.appendChild(script)
    } else {
      MathJax.startup.document.state(0)
      MathJax.texReset()
      MathJax.typesetPromise()
    }
  }

  btf.addGlobalFn('encrypt', loadMathjax, 'mathjax')
  window.pjax ? loadMathjax() : window.addEventListener('load', loadMathjax)
})()</script><script>(() => {
  const runMermaid = ele => {
    window.loadMermaid = true
    const theme = document.documentElement.getAttribute('data-theme') === 'dark' ? 'dark' : 'default'

    ele.forEach((item, index) => {
      const mermaidSrc = item.firstElementChild
      const mermaidThemeConfig = `%%{init:{ 'theme':'${theme}'}}%%\n`
      const mermaidID = `mermaid-${index}`
      const mermaidDefinition = mermaidThemeConfig + mermaidSrc.textContent

      const renderFn = mermaid.render(mermaidID, mermaidDefinition)
      const renderMermaid = svg => {
        mermaidSrc.insertAdjacentHTML('afterend', svg)
      }

      // mermaid v9 and v10 compatibility
      typeof renderFn === 'string' ? renderMermaid(renderFn) : renderFn.then(({ svg }) => renderMermaid(svg))
    })
  }

  const codeToMermaid = () => {
    const codeMermaidEle = document.querySelectorAll('pre > code.mermaid')
    if (codeMermaidEle.length === 0) return

    codeMermaidEle.forEach(ele => {
      const preEle = document.createElement('pre')
      preEle.className = 'mermaid-src'
      preEle.hidden = true
      preEle.textContent = ele.textContent
      const newEle = document.createElement('div')
      newEle.className = 'mermaid-wrap'
      newEle.appendChild(preEle)
      ele.parentNode.replaceWith(newEle)
    })
  }

  const loadMermaid = () => {
    if (true) codeToMermaid()
    const $mermaid = document.querySelectorAll('#article-container .mermaid-wrap')
    if ($mermaid.length === 0) return

    const runMermaidFn = () => runMermaid($mermaid)
    btf.addGlobalFn('themeChange', runMermaidFn, 'mermaid')
    window.loadMermaid ? runMermaidFn() : btf.getScript('https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js').then(runMermaidFn)
  }

  btf.addGlobalFn('encrypt', loadMermaid, 'mermaid')
  window.pjax ? loadMermaid() : document.addEventListener('DOMContentLoaded', loadMermaid)
})()</script></div><script defer src="https://npm.elemecdn.com/jquery@latest/dist/jquery.min.js"></script><script defer data-pjax src="/js/pig.js"></script><script src="/js/weather.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="text-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="站内搜索..." type="text"/></div></div><hr/><div id="local-search-results"></div><div id="local-search-stats-wrap"></div></div></div><div id="search-mask"></div><script src="/js/search/local-search.js"></script></div></div></body></html>